var LGL = window.LGL||{};
window.LGL = LGL;

function getPageUrl(url, pageNo) {
  return url + (url.endsWith('/') ? '' : '/') + 'page/' + pageNo + '/'
}

LGL.initPagination = function(pageObj){
  if (pageObj.data('totalLength') && parseInt(pageObj.data('totalLength')) > 0) {
    var isSearchFunc = 'search' == pageObj.data('type');
    var searchWord = pageObj.data('word');
    var totalLength = parseInt(pageObj.data('totalLength'));
    var itemLimit = parseInt(pageObj.data('itemLimit'));
    var currentPage = parseInt(pageObj.data('currentPage'));
    var totalPage = Math.ceil(totalLength / itemLimit);
    if(totalPage<2){
      return;
    }
    var url = pageObj.data('url');
    url = url ? url.replace(/\/page\/.*/g, '') : '/';
    var pageHtmlArr = [];

    if(currentPage<=1){
      pageHtmlArr.push('<span class="page-btn disabled">上一页</span>');
    }else{
      if(isSearchFunc){
        pageHtmlArr.push('<span data-page="'+(currentPage - 1)+'"  class="page-btn search-page-btn">上一页</span>');
      }else{
        pageHtmlArr.push('<a href="' +getPageUrl(url, currentPage - 1) +'" class="page-btn">上一页</a>');
      }
    }
    var startPage =1;
    if(currentPage-3>2){
      startPage = currentPage-3;
    }
    var endPage = totalPage;
    if(currentPage+3<totalPage-2){
      endPage = currentPage+3;
    }
    
    if(startPage<=3){
      startPage=1;
    }
    if(endPage>=totalPage-2){
      endPage = totalPage;
    }

    if(startPage!=1){
      if(isSearchFunc){
        pageHtmlArr.push('<span data-page="'+(startPage - 1)+'"  class="page-btn search-page-btn">...</span>');
      }else{
        pageHtmlArr.push('<a href="' + getPageUrl(url, startPage - 1) + '" class="page-btn">...</a>')
      }
      
    }

    for(var i=startPage;i<=endPage;i++){
      if(i==currentPage){
        pageHtmlArr.push('<span class="page-btn selected">' +i+'</span>');
      }else{
        if(isSearchFunc){
          pageHtmlArr.push('<span data-page="'+i+'"  class="page-btn search-page-btn">' +i+'</span>');
        }else{
          pageHtmlArr.push('<a href="' +getPageUrl(url, i) +'"class="page-btn">' +i+'</a>');
        }
        
      }
    }
    if(endPage!=totalPage){
      if(isSearchFunc){
        pageHtmlArr.push('<span data-page="'+(endPage + 1)+'"  class="page-btn search-page-btn">...</span>');
      }else{
        pageHtmlArr.push('<a href="' + getPageUrl(url, endPage + 1) + '" class="page-btn">...</a>');
      }
    }
    if(currentPage>=totalPage){
      pageHtmlArr.push('<span class="page-btn disabled">下一页</span>');
    }else{
      if(isSearchFunc){
        pageHtmlArr.push('<span data-page="'+(currentPage + 1)+'"  class="page-btn search-page-btn">下一页</span>');
      }else{
        pageHtmlArr.push('<a href="' +getPageUrl(url, currentPage + 1) +'" class="page-btn">下一页</a>');
      }
    }
    pageObj.html(pageHtmlArr.join(''));

    if(isSearchFunc){
      pageObj.on("click",".search-page-btn",function(){
        LGL.searchFunc(searchWord,$(this).data('page'),itemLimit);
      });
    }
  }
}
